from sqlalchemy import Column, Integer, String, Text, ForeignKey, Numeric, Boolean, DateTime
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from ..base import Base


class ProductWarranty(Base):
    """
    商品保修服务表 - 用于存储产品的保修信息
    """
    __tablename__ = "product_warranty"

    id = Column(Integer, primary_key=True, index=True, comment="保修ID")
    product_id = Column(Integer, ForeignKey("products.id"), nullable=False, comment="商品ID")
    warranty_type = Column(String(100), nullable=False, comment="保修类型")
    warranty_period = Column(String(50), nullable=False, comment="保修期限")
    warranty_desc = Column(Text, nullable=True, comment="保修说明")
    is_active = Column(Boolean, default=True, comment="是否激活")
    sort_order = Column(Integer, default=0, comment="排序顺序")

    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="更新时间")

    # 关联商品
    product = relationship("Product", backref="warranty_services")

    def __repr__(self):
        return f"<ProductWarranty(id={self.id}, product_id={self.product_id}, warranty_type='{self.warranty_type}')>"


class ProductShipping(Base):
    """
    商品物流包装表 - 用于存储产品的物流和包装信息
    """
    __tablename__ = "product_shipping"

    id = Column(Integer, primary_key=True, index=True, comment="物流ID")
    product_id = Column(Integer, ForeignKey("products.id"), nullable=False, comment="商品ID")
    weight = Column(Numeric(8, 2), nullable=True, comment="重量(kg)")
    length = Column(Numeric(8, 2), nullable=True, comment="长度(cm)")
    width = Column(Numeric(8, 2), nullable=True, comment="宽度(cm)")
    height = Column(Numeric(8, 2), nullable=True, comment="高度(cm)")
    package_type = Column(String(100), nullable=True, comment="包装类型")
    package_contents = Column(Text, nullable=True, comment="包装清单")
    shipping_fee = Column(Numeric(10, 2), default=0, comment="运费")
    free_shipping_limit = Column(Numeric(10, 2), nullable=True, comment="包邮门槛")
    shipping_method = Column(String(100), nullable=True, comment="配送方式")

    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="更新时间")

    # 关联商品
    product = relationship("Product", backref="shipping_info")

    def __repr__(self):
        return f"<ProductShipping(id={self.id}, product_id={self.product_id})>"


class ProductService(Base):
    """
    商品服务表 - 用于存储售后服务信息
    """
    __tablename__ = "product_services"

    id = Column(Integer, primary_key=True, index=True, comment="服务ID")
    product_id = Column(Integer, ForeignKey("products.id"), nullable=False, comment="商品ID")
    service_type = Column(String(100), nullable=False, comment="服务类型")
    service_desc = Column(Text, nullable=False, comment="服务说明")
    service_period = Column(String(50), nullable=True, comment="服务期限")
    is_active = Column(Boolean, default=True, comment="是否激活")
    sort_order = Column(Integer, default=0, comment="排序顺序")

    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="更新时间")

    # 关联商品
    product = relationship("Product", backref="services")

    def __repr__(self):
        return f"<ProductService(id={self.id}, product_id={self.product_id}, service_type='{self.service_type}')>"